<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>如何优雅的写出python代码 | 博客</title><meta name="keywords" content="python"><meta name="author" content="小魂"><meta name="copyright" content="小魂"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1如何优美的书写python代码  变量值的交换1a, b &#x3D; b, a  列表推导式 列表推导式是java及C++等语言没有的特性，能够很简洁的实现for循环，可以应用列表，集合或者子字典。例如我们要求20以内的整除3的数的平方的列表，可以用如下代码实现：  1234numbers &#x3D; []for x in xrange(20):    if x % 3 &#x3D;&amp;#x3">
<meta property="og:type" content="article">
<meta property="og:title" content="如何优雅的写出python代码">
<meta property="og:url" content="https://shark-yun.gitee.io/blog/2020/06/09/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.html">
<meta property="og:site_name" content="博客">
<meta property="og:description" content="1如何优美的书写python代码  变量值的交换1a, b &#x3D; b, a  列表推导式 列表推导式是java及C++等语言没有的特性，能够很简洁的实现for循环，可以应用列表，集合或者子字典。例如我们要求20以内的整除3的数的平方的列表，可以用如下代码实现：  1234numbers &#x3D; []for x in xrange(20):    if x % 3 &#x3D;&amp;#x3">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://shark-yun.gitee.io/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png">
<meta property="article:published_time" content="2020-06-09T13:15:50.000Z">
<meta property="article:modified_time" content="2021-02-27T10:04:22.003Z">
<meta property="article:author" content="小魂">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://shark-yun.gitee.io/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://shark-yun.gitee.io/blog/2020/06/09/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-02-27 18:04:22'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"> <script src="/blog/live2d-widget/autoload.js"></script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/blog/img/style/touxiang.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">博客</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">如何优雅的写出python代码</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-06-09T13:15:50.000Z" title="发表于 2020-06-09 21:15:50">2020-06-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-02-27T10:04:22.003Z" title="更新于 2021-02-27 18:04:22">2021-02-27</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">如何优美的书写python代码</span><br></pre></td></tr></table></figure>

<h3 id="变量值的交换"><a href="#变量值的交换" class="headerlink" title="变量值的交换"></a>变量值的交换</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">a, b &#x3D; b, a</span><br></pre></td></tr></table></figure>

<h3 id="列表推导式"><a href="#列表推导式" class="headerlink" title="列表推导式"></a>列表推导式</h3><blockquote>
<p>列表推导式是java及C++等语言没有的特性，能够很简洁的实现for循环，可以应用列表，集合或者子字典。<br>例如我们要求20以内的整除3的数的平方的列表，可以用如下代码实现：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">numbers &#x3D; []</span><br><span class="line">for x in xrange(20):</span><br><span class="line">    if x % 3 &#x3D;&#x3D; 0:</span><br><span class="line">        numbers.append(x*x)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>而通过列表推导式一行代码就可以实现：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">numbers &#x3D; [x*x for x in xrange(20) if x % 3 &#x3D;&#x3D; 0]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>列表推导式可以用于集合和字典，将[…] 变为[…]。集合和字典的实现如下所示：</p>
</blockquote>
<p>集合：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">numbers &#x3D; &#123;x*x for x in range(0, 20) if x % 3 &#x3D;&#x3D; 0&#125;</span><br></pre></td></tr></table></figure>
<p>字典：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">numbers &#x3D; &#123;x: x*x for x in range(0, 20) if x % 3 &#x3D;&#x3D; 0&#125;</span><br></pre></td></tr></table></figure>

<h3 id="字符串拼接"><a href="#字符串拼接" class="headerlink" title="字符串拼接"></a>字符串拼接</h3><blockquote>
<p>这是一个老生常谈的问题，当我们需要将数个字符串拼接起来的时候，习惯性的使用 “+” 作为连接字符串的手段。</p>
</blockquote>
<p>然而，由于像字符串这种不可变对象在内存中生成后无法修改，合并后的字符串会重新开辟一块内存空间来存储。因此没合并一次就会单独开辟一块内存空间，这样会占用大量的内存，严重影响代码的效率.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">words &#x3D;[&#39;I&#39;, &#39; &#39;, &#39;LOVE&#39;, &#39; &#39;, &#39;pYTHON&#39;, &#39;.&#39;]</span><br><span class="line"></span><br><span class="line">sentence &#x3D; &#39;&#39;</span><br><span class="line">for word in words:</span><br><span class="line">    sentence +&#x3D; &#39;&#39; + word</span><br></pre></td></tr></table></figure>
<blockquote>
<p>解决这个问题的办法是使用字符串连接的join，Python写法如下：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">words &#x3D; [&#39;i&#39;, &#39; &#39;, &#39;love&#39;, &#39;python&#39;,&#39;&#39;]</span><br><span class="line"></span><br><span class="line">sentence &#x3D; &#39;&#39;.join(words)</span><br></pre></td></tr></table></figure>





<h3 id="如何快速翻转字符串"><a href="#如何快速翻转字符串" class="headerlink" title="如何快速翻转字符串"></a>如何快速翻转字符串</h3><blockquote>
<p>java 或者 C++ 等语言的写法是新建一个字符串，从最后开始访问员字符串：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a &#x3D; &#39;I love python.&#39;</span><br><span class="line"></span><br><span class="line">reverse_a &#x3D; &#39;&#39;</span><br><span class="line">for i in range(0, len(a)):</span><br><span class="line">    reverse_a +&#x3D; a[len(a) - i - 1]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>而Python则将字符串看做list，而列表可以通过切片操作来实现翻转：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a &#x3D; &quot;I LOVE PYTHON&quot;</span><br><span class="line"></span><br><span class="line">reverse_a &#x3D; a[::-1]</span><br></pre></td></tr></table></figure>
<h3 id="for-else-语句"><a href="#for-else-语句" class="headerlink" title="for/else 语句"></a>for/else 语句</h3><blockquote>
<p>在C语言或java语言中，我们寻找一个字符串是否在一个list中，通常会设置一个布尔型变量表示是否找到：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">cities &#x3D; [&#39;Beijing&#39;, &#39;Shanghai&#39;, &#39;Tianjin&#39;, &#39;Shenzhen&#39;,&#39;Wuhan&#39;]</span><br><span class="line">tofind &#x3D; &#39;Shanghai&#39;</span><br><span class="line"></span><br><span class="line">found &#x3D; False</span><br><span class="line">for city in cities:</span><br><span class="line">    if tofind &#x3D;&#x3D; city:</span><br><span class="line">        print(&#39;Found&#39;)</span><br><span class="line">        found &#x3D; True</span><br><span class="line">        break</span><br><span class="line">if not found:</span><br><span class="line">    print(&#39;Not found!&#39;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<blockquote>
<p>而python中通过for … else … 会使得代码很简洁，注意else中的代码快仅仅是在for循环中没有执行break语句的时候执行：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">cities &#x3D; [&#39;BeiJing&#39;, &#39;TianJin&#39;, &#39;JiNan&#39;, &#39;ShenZhen&#39;, &#39;WuHan&#39;]</span><br><span class="line">tofind &#x3D; &#39;Shanghai&#39;</span><br><span class="line"></span><br><span class="line">for city in citiees:</span><br><span class="line">    if tofind &#x3D;&#x3D; city:</span><br><span class="line">        print(&#39;Found&#39;)</span><br><span class="line">        break</span><br><span class="line">else:</span><br><span class="line">    print(&#39;Not found!&#39;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>另外，while和try关键字都可以和else搭配使用</p>
</blockquote>
<h3 id="迭代对象善用enumerate类"><a href="#迭代对象善用enumerate类" class="headerlink" title="迭代对象善用enumerate类"></a>迭代对象善用enumerate类</h3><blockquote>
<p>enumerate类接收两个参数，其中一个是可以地迭代的对象，另外一个是开始的索引。比如，我们想要的打印一份列表的索引机器内容，可以用如下代码实现：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">cities &#x3D; [&#39;Beijing&#39;, &#39;Shanghai&#39;, &#39;Wuhan&#39;]</span><br><span class="line"></span><br><span class="line">index &#x3D; 0 </span><br><span class="line">for city in cities:</span><br><span class="line">    index &#x3D; index + 1</span><br><span class="line">    print(index, &#39;:&#39;, &#39;city&#39;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>而通过使用enumerate则极大的简化了代码，这里索引设置为从1开始(默认是从 0 开始):</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cities &#x3D; [&#39;Beijing&#39;, &#39;Shanghai&#39;, &#39;Wuhan&#39;]</span><br><span class="line"></span><br><span class="line">for index, ciity in enumerate(cities, 1):</span><br><span class="line">    print(index, &#39;:&#39;, city)</span><br></pre></td></tr></table></figure>
<h3 id="通过lambda来定义函数"><a href="#通过lambda来定义函数" class="headerlink" title="通过lambda来定义函数"></a>通过lambda来定义函数</h3><blockquote>
<p>lambda可以返回一个可以调用的函数对象，会使得代码更为简洁，若不使用lambda则需要单独定义一个函数：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def f(x):</span><br><span class="line">    return x*x</span><br><span class="line"></span><br><span class="line">map(f, [1,2,3,4,5,6,7,8])</span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用lambda后仅仅使用一行代码：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">map(lambda x: x * x,[1,2,3,4,5,6,7,8] )</span><br></pre></td></tr></table></figure>

<blockquote>
<p>这里注意，lambda生成的是一个可以向其他函数一样使用的函数对象，即</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">def f(x);</span><br><span class="line">    return x *x</span><br><span class="line"></span><br><span class="line">等价于</span><br><span class="line"></span><br><span class="line">lambda x ： x*x</span><br></pre></td></tr></table></figure>

<h3 id="应用上下文管理"><a href="#应用上下文管理" class="headerlink" title="应用上下文管理"></a>应用上下文管理</h3><blockquote>
<p>在打开文件时，通常是通过捕获异常来进行实现的，并且在finally模块中对文件来进行关闭：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">try：</span><br><span class="line">    file &#x3D; open(&#39;python.txt&#39;)</span><br><span class="line">    for line in file:</span><br><span class="line">        print(line)</span><br><span class="line">except:</span><br><span class="line">    print(&#39;File error!&#39;)</span><br><span class="line">finally:</span><br><span class="line">    file.close()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>而通过上下文管理中的with语句可以让代码非常简洁：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">with open(&#39;python.txt&#39;) as file:</span><br><span class="line">    for line in file:</span><br><span class="line">        print(line)</span><br></pre></td></tr></table></figure>

<h3 id="使用装饰器"><a href="#使用装饰器" class="headerlink" title="使用装饰器"></a>使用装饰器</h3><blockquote>
<p>装饰器在Python中的应用非常广泛，其特点是可以再具体函数执行之前或者之后做相关的操作。比如：执行前打印执行函数的相关信息，对函数的参数进行校验，执行后记录函数调用的相关日志等，使用装饰器可以用如下代码实现：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">from time import ctime</span><br><span class="line"></span><br><span class="line">def foo():</span><br><span class="line">    print(&quot;[%s] %() is called&quot; % (ctime(), foo.__name__))</span><br><span class="line">    print(&#39;Hello , Python&#39;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样写的问题是业务逻辑中会夹杂参数检查，日志记录等信息，是得代码逻辑不够清晰，所以，这种场合需要使用装饰器：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">from time import ctime </span><br><span class="line"></span><br><span class="line">def deco(func):</span><br><span class="line">    def decorator(*args，**kwargs):</span><br><span class="line">        print(&quot;[%s] %s() is called&quot; % (citme(), func,__name__)</span><br><span class="line">        return  func(*args, **kwargs)</span><br><span class="line">    return decorator</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@deco</span><br><span class="line">def foo():</span><br><span class="line">    print(&quot;Hello Python&quot;)</span><br></pre></td></tr></table></figure>

<h3 id="使用生成器"><a href="#使用生成器" class="headerlink" title="使用生成器"></a>使用生成器</h3><blockquote>
<p>生成器与列表最大的区别就是，列表是一次行生成的，需要较大的内存空间；而生成器是需要的时候生成的，基本不占用内存空间，生成器分为生成器表达式和生成器函数：</p>
</blockquote>
<blockquote>
<p>先看一下列表:</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">l &#x3D; [x for x in range(10)]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>改成生成器只需要将[ .. ]变为（…），即</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">g &#x3D; (x  for x in range(10))</span><br></pre></td></tr></table></figure>
<blockquote>
<p>至于生成器函数，是通过yield关键字来实现的，我们一计算斐斐波那契数列为例，使用列表可以用如下代码来说实现：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">def fib(max):</span><br><span class="line">    n, a, b &#x3D; 0, 0, 1</span><br><span class="line">    fibonacci &#x3D; []</span><br><span class="line">    while n &lt; max:</span><br><span class="line">        fibonacci,append(b)</span><br><span class="line">        a, b &#x3D; b, a + b</span><br><span class="line">        n &#x3D; n + 1</span><br><span class="line">    return fibonacci</span><br></pre></td></tr></table></figure>

<blockquote>
<p>而使用生成器则变为：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">def fib(max);</span><br><span class="line">    n, a, b &#x3D; 0, 0, 1</span><br><span class="line">    while n &lt; max:</span><br><span class="line">        yield b</span><br><span class="line">        a, b &#x3D; b, a+b</span><br><span class="line">        n &#x3D; n + 1</span><br></pre></td></tr></table></figure>
<h3 id="counter的使用"><a href="#counter的使用" class="headerlink" title="counter的使用"></a>counter的使用</h3><blockquote>
<p>通常的词频统计中，我们的思路是：</p>
</blockquote>
<blockquote>
<p>需要一个字典，key值存储单词，value存储对应的词频。当遇到一个单词，判断是否在这个字典中，如果是，则词频加1，如果否，则字典中新增这个单词，同时对应的词频设置为1.</p>
</blockquote>
<blockquote>
<p>对应的Python代码实现如下：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"># 统计单词出现的次数</span><br><span class="line">def computeFrequencies(wordList):</span><br><span class="line">    # 词频字典</span><br><span class="line">    wordfrequencies &#x3D; &#123;&#125;</span><br><span class="line">    </span><br><span class="line">    for word in wordList:</span><br><span class="line">        if word not in wordfrequescies;</span><br><span class="line">        #单词不在单词词频字典中，词频设置为1</span><br><span class="line">            wordfrequescies[word] &#x3D; 1</span><br><span class="line">        else:</span><br><span class="line">            # 单词在单词词频字典中，词频加   1</span><br><span class="line">            wordfrequencies[word] +&#x3D; 1</span><br><span class="line">    return wordfrequencies</span><br></pre></td></tr></table></figure>
<blockquote>
<p>有没有更简单的方法吗？答案是肯定的，就是使用Counter。collection中的Counter类就完成了这样的功能，他是字典类的一个子类。Python代码变得无比简洁：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 统计单词出现的频次</span><br><span class="line">def computeFrequencies(wordList):</span><br><span class="line">    # 词频字典</span><br><span class="line">    wordfrequencies &#x3D; Counter(wordList)</span><br><span class="line">    return wordfrequencies</span><br></pre></td></tr></table></figure>
<h3 id="链式比较"><a href="#链式比较" class="headerlink" title="链式比较"></a>链式比较</h3><blockquote>
<p>在实际的数字比较中，我们可能需要多次比较多次，比如我们判断学习成绩是否位与某个区间：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">x &#x3D; 79</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; x &lt; 80 and x &gt; 70</span><br><span class="line">True</span><br></pre></td></tr></table></figure>
<blockquote>
<p>变更Pythonic的写法变身链式比较:</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">x  &#x3D; 79</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; 80 &lt; x 90</span><br><span class="line">False</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; 70 &lt; x &lt; 80</span><br><span class="line">True</span><br></pre></td></tr></table></figure>

<h3 id="函数返回多个值"><a href="#函数返回多个值" class="headerlink" title="函数返回多个值"></a>函数返回多个值</h3><blockquote>
<p>在java语言中，当函数需要返回多个值时，通常的做法是生成一个Response对象，然后将要返回的值写入对象内部，而Python不需要这样做。可以直接返回多个值：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def f();</span><br><span class="line">    error_code &#x3D; 0</span><br><span class="line">    error_desc &#x3D; &quot;成功&quot;</span><br><span class="line">    return error_code, error_desc</span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用的时候也会非常简单：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">code， desc &#x3D; f()</span><br><span class="line">print(code, desc)</span><br></pre></td></tr></table></figure>

<h3 id="使用-运算符"><a href="#使用-运算符" class="headerlink" title="使用*运算符"></a>使用*运算符</h3><blockquote>
<p>*运算符和 ** 运算符完美的解决了将元组参数、字典参数进行 unpack,从而简化了函数定义的形式，如：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">def fun(*args):</span><br><span class="line">    for eacharg in args:</span><br><span class="line">        print(&#39;tuple arg:&#39;, eacharg)</span><br><span class="line"></span><br><span class="line">fun(&#39;I&#39;, &#39;LOVE&#39;, &#39;PYTHON&#39;)</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">运行的结果：</span><br><span class="line">tuple arg: I</span><br><span class="line"></span><br><span class="line">tuple arg: love</span><br><span class="line"></span><br><span class="line">tuple arg: Python</span><br></pre></td></tr></table></figure>

<h3 id="找出列表中出现最多的数："><a href="#找出列表中出现最多的数：" class="headerlink" title="找出列表中出现最多的数："></a>找出列表中出现最多的数：</h3><blockquote>
<p>这是经常遇到的一个问题。解决这个问题的其中一个思路是按照标题 11 提供的词频统计的方法，先统计词频，然后遍历字典，找出具有最大词频的数字，有没有简洁的方式？</p>
</blockquote>
<blockquote>
<p>当然，Python代码入下：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">num &#x3D; [1,2,3,4,4,4,4, 2,3,4,5,6,7,8,9]</span><br><span class="line"></span><br><span class="line">ptint(max(set(num), key&#x3D;num.count)) </span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">小魂</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://shark-yun.gitee.io/blog/2020/06/09/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/">https://shark-yun.gitee.io/blog/2020/06/09/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://shark-yun.gitee.io/blog" target="_blank">博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/python/">python</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/" target="_blank"><img class="post-qr-code-img" src="/blog/"/></a><div class="post-qr-code-desc"></div></li><li class="reward-item"><a href="/blog/" target="_blank"><img class="post-qr-code-img" src="/blog/"/></a><div class="post-qr-code-desc"></div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2020/06/15/git%E6%95%99%E7%A8%8B/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E5%A4%A7%E5%85%A8/"><img class="prev-cover" src="/blog/img/orcal%E6%95%B0%E6%8D%AE%E5%BA%93%E6%97%B6%E9%97%B4%E8%AE%BE%E7%BD%AE%E9%97%AE%E9%A2%98/index.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">git常用命令大全</div></div></a></div><div class="next-post pull-right"><a href="/blog/2020/06/09/git%E6%95%99%E7%A8%8B/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E5%A4%A7%E5%85%A8/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/"><img class="next-cover" src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">如何优雅的写出python代码</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2019/12/17/opencv/opencv_split_img/" title="opencv切割图片"><img class="cover" src="/blog/img/opencv/opencv_split_img/opencv_split_img_style.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-17</div><div class="title">opencv切割图片</div></div></a></div><div><a href="/blog/2020/06/09/git教程/git常用命令大全/如何优雅的写出python代码/" title="如何优雅的写出python代码"><img class="cover" src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-09</div><div class="title">如何优雅的写出python代码</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/blog/img/style/touxiang.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">小魂</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">26</div></a></div><div class="card-info-data-item is-center"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/shangxiaohun/dashboard/projects" target="_blank" title="Github"><i class="fa fa-github"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E5%80%BC%E7%9A%84%E4%BA%A4%E6%8D%A2"><span class="toc-number">1.</span> <span class="toc-text">变量值的交换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E6%8E%A8%E5%AF%BC%E5%BC%8F"><span class="toc-number">2.</span> <span class="toc-text">列表推导式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5"><span class="toc-number">3.</span> <span class="toc-text">字符串拼接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E5%BF%AB%E9%80%9F%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">4.</span> <span class="toc-text">如何快速翻转字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#for-else-%E8%AF%AD%E5%8F%A5"><span class="toc-number">5.</span> <span class="toc-text">for&#x2F;else 语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1%E5%96%84%E7%94%A8enumerate%E7%B1%BB"><span class="toc-number">6.</span> <span class="toc-text">迭代对象善用enumerate类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%9A%E8%BF%87lambda%E6%9D%A5%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0"><span class="toc-number">7.</span> <span class="toc-text">通过lambda来定义函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BA%94%E7%94%A8%E4%B8%8A%E4%B8%8B%E6%96%87%E7%AE%A1%E7%90%86"><span class="toc-number">8.</span> <span class="toc-text">应用上下文管理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="toc-number">9.</span> <span class="toc-text">使用装饰器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%94%9F%E6%88%90%E5%99%A8"><span class="toc-number">10.</span> <span class="toc-text">使用生成器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#counter%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">11.</span> <span class="toc-text">counter的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%93%BE%E5%BC%8F%E6%AF%94%E8%BE%83"><span class="toc-number">12.</span> <span class="toc-text">链式比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%A4%9A%E4%B8%AA%E5%80%BC"><span class="toc-number">13.</span> <span class="toc-text">函数返回多个值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">14.</span> <span class="toc-text">使用*运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%BE%E5%87%BA%E5%88%97%E8%A1%A8%E4%B8%AD%E5%87%BA%E7%8E%B0%E6%9C%80%E5%A4%9A%E7%9A%84%E6%95%B0%EF%BC%9A"><span class="toc-number">15.</span> <span class="toc-text">找出列表中出现最多的数：</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/04/21/%E6%98%BE%E7%A4%BA%E5%99%A8/xianshiqi/" title="Linux开启显示器内置声卡"><img src="/blog/img/%E6%98%BE%E7%A4%BA%E5%99%A8/picture/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Linux开启显示器内置声卡"/></a><div class="content"><a class="title" href="/blog/2021/04/21/%E6%98%BE%E7%A4%BA%E5%99%A8/xianshiqi/" title="Linux开启显示器内置声卡">Linux开启显示器内置声卡</a><time datetime="2021-04-21T05:08:50.000Z" title="发表于 2021-04-21 13:08:50">2021-04-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/02/09/%E6%95%99%E7%A8%8B/%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87/" title="环境准备"><img src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="环境准备"/></a><div class="content"><a class="title" href="/blog/2021/02/09/%E6%95%99%E7%A8%8B/%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87/" title="环境准备">环境准备</a><time datetime="2021-02-09T02:59:50.000Z" title="发表于 2021-02-09 10:59:50">2021-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/02/07/%E6%95%99%E7%A8%8B/%E5%88%86%E6%9E%90%E5%BB%BA%E5%BA%93%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C/" title="分析建库准备工作"><img src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="分析建库准备工作"/></a><div class="content"><a class="title" href="/blog/2021/02/07/%E6%95%99%E7%A8%8B/%E5%88%86%E6%9E%90%E5%BB%BA%E5%BA%93%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C/" title="分析建库准备工作">分析建库准备工作</a><time datetime="2021-02-07T08:15:50.000Z" title="发表于 2021-02-07 16:15:50">2021-02-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/09/27/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/" title="送给小学弟的一封信"><img src="/blog/img/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%86%99%E5%87%BApython%E4%BB%A3%E7%A0%81/index.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="送给小学弟的一封信"/></a><div class="content"><a class="title" href="/blog/2020/09/27/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/%E9%80%81%E7%BB%99%E5%B0%8F%E5%AD%A6%E5%BC%9F%E7%9A%84%E4%B8%80%E5%B0%81%E4%BF%A1/" title="送给小学弟的一封信">送给小学弟的一封信</a><time datetime="2020-09-26T18:20:50.000Z" title="发表于 2020-09-27 02:20:50">2020-09-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/07/11/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/" title="傅立叶变换"><img src="/blog/img/opencv/opencv_split_img/opencv_split_img_style.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="傅立叶变换"/></a><div class="content"><a class="title" href="/blog/2020/07/11/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/%E5%82%85%E7%AB%8B%E5%8F%B6%E5%8F%98%E6%8D%A2/" title="傅立叶变换">傅立叶变换</a><time datetime="2020-07-11T15:53:50.000Z" title="发表于 2020-07-11 23:53:50">2020-07-11</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 小魂</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">你只管努力，剩下的交给时间</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>